package com.twp.auth.rest.api.manage;

import com.twp.auth.domain.authority.user.ManageUser;
import com.twp.auth.service.user.ManageUserService;
import com.twp.auth.tuple.UserRoleActionBody;
import com.twp.common.component.process.exception.combo.IComboExecptionForFunc;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;


@Slf4j
@RestController
@RequestMapping(value = "/api/oauth/manageuser",produces = "application/json; charset=utf-8")
public class ManageUserController {

    @Autowired
    IComboExecptionForFunc execptionForFunc;

    @Autowired
    ManageUserService manageUserService;

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    @AllArgsConstructor@NoArgsConstructor@Data
    public static class FindBody{
        /*
        手机号，用户昵称，用户工号
         */
        private String phoneNumber;
        private String name;
        private String userName;
    }

    /*
     根据条件去查询顾客列表
     手机号码,用户名称，用户账号
     */
    //@MethodInfo(code = "ManageUserController_find")
    @GetMapping("")
    ResultDTO find(FindBody findBody,@PageableDefault Pageable pageable){
        ResultDTO result = ResultDTO.builder().build();
        execptionForFunc.executeFlow(()->{
            Page<ManageUser> data = manageUserService.find(findBody,pageable);
            if (data.hasContent()){
                data.getContent().forEach(
                        u-> Optional.ofNullable(u.getDetail()).ifPresent(
                                it->it.setManageUser(null)
                        )
                );
            }
            result.setData(data);
        },e->{
            result.setIsSuccess(false);
            result.setMessage("根据条件去查询顾客列表失败");
        });
        return result;
    }

    /*
    编辑用户资料
     */
    //@MethodInfo(code = "ManageUserController_add_edit")
    @PostMapping("")
    ResultDTO edit(@RequestBody ManageUser manageUser){
        ResultDTO result = ResultDTO.builder().build();
        execptionForFunc.executeFlow(
                ()->{
                    manageUserService.saveOrUpdate(manageUser);
                    result.setMessage("更新用户信息成功");
                },
                e->{
                    result.setMessage("更新用户信息失败");
                    result.setIsSuccess(false);
                }
        );
        return result;
    }

    /*
    删除用户信息
     */
    //@MethodInfo(code = "ManageUserController_delete")
    @DeleteMapping("/{id}")
    ResultDTO delete(@PathVariable String id){
        ResultDTO result = ResultDTO.builder().build();
        execptionForFunc.executeFlow(
                ()->manageUserService.deleteById(Integer.parseInt(id)),
                e->{
                    result.setMessage("删除用户信息失败");
                    result.setIsSuccess(false);
                }
        );
        return result;
    }


    /*
    取消用户与角色的绑定
    增加用户与角色绑定
     */
    @PutMapping("action")
    ResultDTO action(@RequestBody UserRoleActionBody actionBody){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("操作用户与角色绑定").build(),
                it-> manageUserService.action(actionBody),
                (e,it)->{
                    it.setMessage("操作用户与角色绑定异常");
                    it.setIsSuccess(false);
                }
        );
    }



}
